/** 
 * <p>Original Author:  jessefreeman</p>
 * <p>Class File: PhotoStack.as</p>
 * 
 * <p>Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:</p>
 * 
 * <p>The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.</p>
 * 
 * <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.</p>
 * 
 * <p>Licensed under The MIT License</p>
 * <p>Redistributions of files must retain the above copyright notice.</p>
 *
 * <p>Revisions<br/> 
 * 	0.1  Initial version Nov 18, 2008</p>
 *
 */

package camo.views.components.stacks
{
	import camo.controllers.helpers.ImagePreloaderHelper;
	import camo.models.IModel;
	import camo.views.components.AbstractComponent;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class PhotoStack extends AbstractComponent
	{
		
		protected var _preloadHelper:ImagePreloaderHelper;
		
		protected var _currentID:Number = -1;
		protected var _currentImage:Bitmap;
		protected var _debugMode:Boolean = false;
		public var loaded:Boolean = false;
		protected var _activeImage:DisplayObject;
		protected var _tempImage:DisplayObject;
		protected var _imageContainer:Sprite;
		
		/**
		 * 
		 * @return 
		 * 
		 */		
		public function get nextID():Number{
			var tempID:Number = _currentID + 1;
			
			_currentID = (tempID > (_preloadHelper.instances.length-1)) ? 0 : tempID;
			
			return _currentID;
		}
		
		/**
		 * 
		 * @return 
		 * 
		 */		
		public function get prevID():Number{
			var tempID:Number = _currentID - 1;
			
			_currentID = (tempID < 0) ? (_currentID - 1) : tempID;
			
			return _currentID;
		}
		
		/**
		 * 
		 * @return 
		 * 
		 */		
		public function get total():Number{
			return (_preloadHelper.instances) ? _preloadHelper.instances.length : 0;
		}
		
		
		/**
		 * 
		 * @param self
		 * @param model
		 * 
		 */		
		public function PhotoStack(model:IModel)
		{
			super(this, model);
			
		}
		
		/**
		 * 
		 * @param model
		 * 
		 */
		override protected function init(model:IModel):void{
			
			super.init(model);
			_imageContainer = new Sprite();
			addToDisplay(_imageContainer);
			
			_preloadHelper = new ImagePreloaderHelper();
			_preloadHelper.defaultDelimiter = "=";
			
			if(_debugMode){
				addEventListener(MouseEvent.CLICK, onDebugClick);
			}
		}
		
		/**
		 * 
		 * @param e
		 * 
		 */		
		internal function onDebugClick(e:MouseEvent):void{
			e.stopImmediatePropagation();
			nextPhoto();
		}
		
		/**
		 * 
		 * @param urls
		 * 
		 */		
		public function load(urls:Array):void{
			_preloadHelper.addEventListener(Event.COMPLETE,onLoadComplete);
			_preloadHelper.addToQueue(urls);
		}
		
		/**
		 * 
		 * @param e
		 * 
		 */		
		protected function onLoadComplete(e:Event):void{
			e.stopImmediatePropagation();
			loaded = true;
			dispatchEvent(new Event(Event.COMPLETE,true,true));
		}
		
		/**
		 * 
		 * @param id
		 * 
		 */		
		public function gotoPhoto(id:Number, externalFlag:Boolean = true):void{
			
			if(_currentID != id)
				 _currentID = id;
			//var tempImage:Bitmap
			if(_activeImage)
				_imageContainer.removeChild(_activeImage);
			
			_activeImage = _preloadHelper.instances[id];
			
			if(_activeImage)
				_imageContainer.addChild(_activeImage);
		}

		/**
		 * 
		 * 
		 */		
		public function nextPhoto():void{
			gotoPhoto(nextID);
		}
		
		/**
		 * 
		 * 
		 */		
		public function previousPhoto():void{
			gotoPhoto(prevID);
		}
		
	}
}